package com.customers.util;

import com.customers.entity.User;
import org.apache.xpath.objects.XString;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class EncapsulationUtil {
    public static PreparedStatement objectIntoPs(Object object, String sql, PreparedStatement ps) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, SQLException {
        String newsql = sql.replaceAll("#\\{[a-zA-Z0-9_$]*}", "?");
        ps = ConnectJDBC.connectDB(newsql);
        int fromIndex = 0;
        int Index = 1;
        while(true){
            int jinIndex = sql.indexOf("#", fromIndex);
            if(jinIndex < 0)   break;
            int kuoIndex = sql.indexOf("}", fromIndex);
            String propertyName = sql.substring(jinIndex + 2, kuoIndex).trim();
            fromIndex = kuoIndex + 1;
            String getMethodName = "get" + propertyName.toUpperCase().charAt(0) + propertyName.substring(1);
            Method getMethod = object.getClass().getDeclaredMethod(getMethodName);
            Object propertyValue = getMethod.invoke(object);
            ps.setString(Index,propertyValue.toString());
            Index++;
        }
        return ps;
    }

    public static void customer(){}

}
